#!/bin/sh
#
# webvnc -- open the docker lab's novnc frontend via a browser
#
# Copyright (C) 2016-2021 Wu Zhangjin <falcon@ruma.tech>
#

TOP_DIR="$(cd "$(dirname "$0")"/../../ && pwd)"
. "$TOP_DIR"/tools/docker/config >/dev/null

# lab info
show_lab()
{
echo "Please login via VNC Client with:"
echo
echo "          IP: $VNC_IP"
echo "        User: $VNC_TOKEN (Only for noVNC)"
echo "    Password: $VNC_PWD (Normal)"
echo "    Password: $VNC_PWD_VIEWONLY (View)"
echo
echo "Login for noVNC web client:"
echo
echo "  * Normal: $HOST_WEB_URL"
echo "  *   View: $HOST_WEB_URL_VIEWONLY"
echo
echo "Login for local VNC clients:"
echo
echo "  *     IP: $VNC_IP"
echo "  * Normal: $VNC_PWD"
echo "  *   View: $VNC_PWD_VIEWONLY"
echo
echo "The Other Login methods:"
echo
echo "  *   Bash: `echo $DOCKER_BASH_CMD | sed -e \"s%$TOP_DIR/%%g\"` $CURRENT"
echo "  *    SSH: `echo $DOCKER_SSH_CMD | sed -e \"s%$TOP_DIR/%%g\"` $CURRENT"
echo "  * WebSSH: `echo $DOCKER_WEBSSH_CMD | sed -e \"s%$TOP_DIR/%%g\"` $CURRENT"
echo

if [ "x$HOST_OS" = "xLinux" ]; then
  ps -eo args | grep -v grep | grep -q "sshpass"
  if [ $? -eq 0 ]; then
    [ -z "$REMOTE_TOKEN_MAP" ] && prepare_export_files

    # Get across access address
    VNC_TOKEN_ACROSS=$(sed -ne "`echo $VNC_IP | cut -d '.' -f4`p" $REMOTE_TOKEN_MAP | cut -d ':' -f1)
    ACROSSHOST=$(ps -eo args | grep -v grep | grep "sshpass"  | head -1 | sed -e "s/.*ubuntu@\([^ ]*\) .*/\1/g")
    HOST_WEB_URL_ACROSS="$(echo "$HOST_WEB_URL" | sed -e "s%$VNC_TOKEN%$VNC_TOKEN_ACROSS%g;s%$HOST%$ACROSSHOST%g")"
    HOST_WEB_URL_VIEWONLY_ACROSS="$(echo "$HOST_WEB_URL_VIEWONLY" | sed -e "s%$VNC_TOKEN%$VNC_TOKEN_ACROSS%g;s%$HOST%$ACROSSHOST%g")"

    echo
    echo "Across URL:"
    echo
    echo "  * Normal: $HOST_WEB_URL_ACROSS"
    echo "  *   View: $HOST_WEB_URL_VIEWONLY_ACROSS"
    echo
  fi
fi

echo "Note: If running on a remote server, use external ip or domain name instead of 'localhost':"
echo
echo "      $ echo \$ip_or_domain_name > '$LAB_HOST'"
echo "      $ tools/docker/release $LAB_NAME"
echo

echo
echo "Note: firefox, safari and edge work, but google chrome web browser is preferable."
echo

# For web portmapping
[ -f "$LAB_PORTMAP" ] \
  && log_print "Check web portmapping ..." \
  && grep -q ":8" "$LAB_PORTMAP" \
  && (docker logs $CONTAINER_NAME 2>/dev/null | grep "Server address:") \
  && for port in $(grep ":8" "$LAB_PORTMAP" | cut -d':' -f1); \
       do info_print "Local Web Address: http://$HOST:$port/"; done

}

resize_screen ()
{
  [ -f /etc/environment.d/90linux-lab-disk.conf ] && . /etc/environment.d/90linux-lab-disk.conf
  if [ -n "$SCREEN_RESIZE" ]; then
    log_print "Resize the screen as request"
    (sleep 0.5; "$DOCKER_RESIZE_CMD" $LAB_NAME $SCREEN_RESIZE >/dev/null 2>&1)&
  fi
}

prepare_login || exit 0

# Enable autoconnect by default
[ -z "$VNC_AUTOCONNECT" ] && VNC_AUTOCONNECT=1

[ $VNC_AUTOCONNECT -eq 1 ] && \
  AUTOCONNECT="&autoconnect=1"

log_print "Get cached data"
get_vars VNC_IP VNC_TOKEN VNC_PWD VNC_PWD_VIEWONLY
get_host

# Get vnc page
HOST_WEB_URL="$WEB_HTTP://${HOST}:${HOST_VNC_PORT}/?u=$VNC_TOKEN&p=$VNC_PWD$AUTOCONNECT"
HOST_WEB_URL_VIEWONLY="$WEB_HTTP://${HOST}:${HOST_VNC_PORT}/?r=${VNC_TOKEN}${VNC_PWD_VIEWONLY}"

show_lab
resize_screen
browse_url "$HOST_WEB_URL"
